package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;

public class LibraryManager {

    private Library library;
    private PrintStream printStream;
    private BufferedReader bufferedReader;
    private static final String TITLE_PROMPT = "Enter title: ";
    private static final String NAME_PROMPT = "Enter name: ";
    private static final String CHECKOUT_SUCCESS_PROMPT = "Book was successfully checked out";
    private static final String CHECKOUT_FAILURE_PROMPT = "Book was not successfully checked out";

    public LibraryManager(Library library, PrintStream printStream, BufferedReader bufferedReader) {
        this.library = library;
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
    }

    public void checkOutBook() throws IOException {
        String title = getInputFromPrompt(TITLE_PROMPT);
        Book book = library.findItem(title);
        if (book != null) {
            String patron = getInputFromPrompt(NAME_PROMPT);
            book.checkOut(patron);
            printStream.println(CHECKOUT_SUCCESS_PROMPT);
        } else {
            printStream.println(CHECKOUT_FAILURE_PROMPT);
        }
    }

    public void returnItem() throws IOException {
        String title = getInputFromPrompt(TITLE_PROMPT);
        String input = bufferedReader.readLine();
        library.findItem(input);
    }

    public void listAvailableBooks() {
        library.listAvailableBooks();
    }

    private String getInputFromPrompt(String prompt) throws IOException {
        printStream.println(prompt);
        return bufferedReader.readLine();
    }
}
